package com.numbfish.ch16_ex08_r2dbc_web_flux;

import com.numbfish.ch16_ex08_r2dbc_web_flux.pojo.Person;
import com.numbfish.ch16_ex08_r2dbc_web_flux.repository.BookRepository;
import com.numbfish.ch16_ex08_r2dbc_web_flux.repository.PersonRepository;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.data.r2dbc.core.R2dbcEntityTemplate;
import org.springframework.data.relational.core.query.Criteria;
import org.springframework.data.relational.core.query.Query;
import org.springframework.r2dbc.core.DatabaseClient;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.io.IOException;
import java.util.Arrays;

@SpringBootTest
@Slf4j

public class Ch16Ex08R2dbcWebFluxTestApp {
    @Autowired
    private  ApplicationContext applicationContext;
    // join操作不好做
    @Autowired
    R2dbcEntityTemplate r2dbcEntityTemplate;

    // join操作好做
    @Autowired
    DatabaseClient databaseClient;

    @Autowired
    PersonRepository personRepository;

    @Autowired
    BookRepository bookRepository;

    @Test
    void contextLoads() {
        log.info("applicationContext = {}",applicationContext);
    }
    @Test
    void beanCount(){
        // 用户未定义任何类时的bean个数为 springBoot2.7 : 139
        // 用户未定义任何类时的bean个数为 springBoot3   : 152
        log.info("applicationContext default bean count= {}",applicationContext.getBeanDefinitionNames().length);
    }


    void outputTheBeanFromContext(Class<?> clazz){
        // 用户未定义任何类时的bean个数为152
        log.info("applicationContext get a bean  {} === {}",clazz,applicationContext.getBean(clazz));
    }

    // 为什么没有按照条件查询
    @Test
    void test1() throws IOException {
        Criteria criteria = Criteria.empty();
        criteria.and("id").is(1L).and("name").is("Alice");
        Query query = Query.query(criteria);
        r2dbcEntityTemplate.select(query, Person.class).subscribe(person -> {
            System.out.println(person);
        });
        System.in.read();
    }

    @Test
    void test2() throws IOException {
        databaseClient.sql("select * from person where id=?").bind(0,1L).fetch().all().map(map->{

            return new Person(Integer.parseInt(map.get("id").toString()),map.get("name").toString(),Integer.parseInt(map.get("age").toString()));
        }).subscribe(person -> System.out.println(person));
        System.in.read();
    }

    // 简单查询
    @Test
    void test3() throws IOException {
        personRepository.findAll().subscribe(person -> {
            System.out.println(person);
        });
        System.in.read();
    }

    // 自定义 personRepository 接口
    // where id = ? and name like
    @Test
    void test4() throws IOException {
        personRepository.findAllByIdInAndNameLike(Arrays.asList(1,2),"%").subscribe(person -> {
            System.out.println(person);
        });
        System.in.read();
    }

    @Test
    void test5() throws IOException {
        personRepository.findHaha().subscribe(person -> {
            System.out.println(person);
        });
        System.in.read();
    }

    @Test
    void test6() throws IOException {
        bookRepository.findHaha(2).subscribe(book -> {
            System.out.println(book);
        });
        System.in.read();
    }


}

